import { Button, Card, message } from 'antd';
import React, { useState, useEffect } from 'react';

import * as api from '@/services/GatewayNewsMgr';
import _ from 'lodash';
import LayoutPage, { ProTitle } from './components/LayoutPage';
import { getCoreWarnInfoList } from '../ValueManage/PriceManage/Approve/utils/treeUtils';
import WarningBriefing from '../ValueManage/PriceManage/Approve/components/WarningBriefing';
import IndicatorImpact from '../ValueManage/PriceManage/Approve/components/IndicatorImpact';
import PriceAdjustmentBuilding from '../ValueManage/PriceManage/Approve/components/PriceAdjustmentBuilding';

// 货值预警待办信息
export default function WarningToDoInfo(props) {
  const { instanceId } = props.location.query;
  const [btnLoading, setBtnLoading] = useState(false);
  const [pageLoading, setPageLoading] = useState(false);
  const [dataState, setDataState] = useState({});
  const isBrowserType = /Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent);
  const requestData = async () => {
    setPageLoading(true);
    const res = await api.commonGetWarnPcToDoGET({ params: { instanceId } });
    if (res && res?.code === 1) {
      // 本次调价楼栋
      const bulidWarnInfoList = {
        marsVersions: res?.data.bulidWarnInfoList?.map(item => item.marsVersion),
        list: res?.data.bulidWarnInfoList,
      };
      const coreWarnInfoList = {
        marsVersions: res?.data.coreWarnInfoList?.map(item => item.marsVersion),
        list: getCoreWarnInfoList(res?.data.coreWarnInfoList),
      };
      setDataState({ ...res?.data, coreWarnInfoList, bulidWarnInfoList });
    }
    setPageLoading(false);
  };

  const handlerSubmitOK = async () => {
    setBtnLoading(true);
    if (isBrowserType) {
      const result = await api.cleanAppToDoUsingGET({
        params: {
          instanceId,
          operatorType: '4', // 操作类型 (3-删除代办 4-待办转已办 )
          projectId: '',
          todoTypeCode: '',
        },
      });
      message.loading('处理预警待办中...', 2, () => {
        if (result?.code === 1) {
          message.success(result.message, 1, () => {
            window.location.href = process.env.protal;
          });
        }
      });
    } else {
      const result = await api.cleanPcToDoUsingGET({
        params: {
          instanceId,
          operatorType: '4', // 操作类型 (3-删除代办 4-待办转已办 )
          projectId: '',
          todoTypeCode: '',
        },
      });
      message.loading('处理预警待办中...', 2, () => {
        if (result?.code === 1) {
          message.success(result.message, () => {
            if (navigator.userAgent.indexOf('MSIE') > 0) {
              // close IE
              if (navigator.userAgent.indexOf('MSIE 6.0') > 0) {
                window.opener = null;
                window.close();
              } else {
                window.open('', '_top');
                window.top.close();
              }
            } else {
              // close chrome;It is effective when it is only one.
              window.location.href = '';
              window.open('', '_self');
              window.close();
            }
          });
        }
      });
    }
    setBtnLoading(false);
  };

  // 初始化
  useEffect(requestData, []);

  return (
    <LayoutPage
      header={dataState?.name || ''}
      footer={
        <>
          <Button
            type="primary"
            loading={btnLoading}
            onClick={handlerSubmitOK}
            disabled={dataState?.todoStatus !== '0'}
          >
            知道了
          </Button>
          {!isBrowserType && !_.isEmpty(dataState.billUrl) ? (
            <>
              &nbsp;
              <Button type="primary" onClick={() => window.open(dataState.billUrl)}>
                查看货值审批单
              </Button>
            </>
          ) : null}
        </>
      }
    >
      <Card>
        <ProTitle>预警信息</ProTitle>
        <WarningBriefing
          warnFlag
          data={dataState?.bulletinWarnInfo || []}
          loading={pageLoading}
          contrastType={dataState?.contrastType}
        />
        <br />
        <IndicatorImpact
          data={dataState?.coreWarnInfoList || []}
          loading={pageLoading}
          contrastType={dataState?.contrastType}
        />
        <br />
        <PriceAdjustmentBuilding
          data={dataState?.bulidWarnInfoList || []}
          loading={pageLoading}
          contrastType={dataState?.contrastType}
        />
      </Card>
    </LayoutPage>
  );
}
